import { handleTimes } from '@/utils/utils';
import { ChargeFrameItem } from './types.d';

export const getDayHour = (times: (number | string | Date | moment.Moment)[]) => {
  if (times.length != 2) return 23;
  const [start, end] = handleTimes(times);
  if (start == end) return 23;
  const frame = start - end > 0 ? start - end : 1440 + (start - end);
  const h = Math.ceil(frame / 60);
  return h > 23 ? 23 : h;
};

export const transformFrames = (
  chargeFrames: ChargeFrameItem[],
  isSection: boolean,
  sectionTime: number,
) => {
  if (!isSection) return chargeFrames;
  return chargeFrames.map((item) => {
    const { start, end, price } = item;
    // let price = item.price;
    // if (index != 0) {
    //   price = price - array[index - 1].price;
    // }
    // const delt = (end - start) * (60 / sectionTime);
    // const dp = Number((price / delt).toFixed(2));
    return {
      start,
      end,
      unit: sectionTime,
      price,
    };
  });
};

export const mapedFrames = (
  chargeFrames: ChargeFrameItem[],
  isSection: boolean,
  // dayMax: number,
) => {
  if (!isSection) return { sectionTime: 30, chargeFrames };
  const sectionTime = chargeFrames[0]?.unit || 30;
  // const mapedFrames = chargeFrames.map((item) => {
  //   const { start, end, unit, price } = item;
  //   // const tp = (end - start + 1) * (60 / unit) * price;
  //   // sectionTime = unit;
  //   // initP += tp;
  //   return {
  //     start,
  //     end,
  //     unit,
  //     price,
  //   };
  // });
  return { sectionTime, chargeFrames };
};
